package cn.yunhe.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import sun.dc.pr.PRError;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class SqlSessionFactoryUtil {

    private final static Class<SqlSessionFactoryUtil> LOCK = SqlSessionFactoryUtil.class;

    private static SqlSessionFactory sqlSessionFactory = null;

    private SqlSessionFactoryUtil() {
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        synchronized (LOCK) {
            if (sqlSessionFactory != null) {
                return sqlSessionFactory;
            }

            String resource = "mybatis.xml";
            InputStream inputStream;
            try {
                inputStream = Resources.getResourceAsStream(resource);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

                //采用程序传递加密参数
                InputStream in = Resources.getResourceAsStream("jdbc.properties");
                Properties props = new Properties();
                props.load(in);
                String username = props.getProperty("database.username");
                String password = props.getProperty("database.password");
                //解密用户名和密码，并在属性中重置
                props.put("database.username", CodeUtils.decode(username));
                props.put("database.password", CodeUtils.decode(password));

                inputStream = Resources.getResourceAsStream(resource);

                //使用程序传递的方式覆盖原有的properties属性参数
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, props);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return sqlSessionFactory;
        }
    }


    public static SqlSession getSqlSession() {
        if (sqlSessionFactory == null) {
            getSqlSessionFactory();
        }
        return sqlSessionFactory.openSession();
    }


    /**
     * 加密后的字符串放入jdbc.properties
     *
     * @param
     */
/*    public static void main(String[] args) {
        System.out.println(CodeUtils.encode("root"));
        System.out.println(CodeUtils.encode("000000"));
    }*/

    static class CodeUtils {
        private static String encode(String src) {
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(src.getBytes());
        }

        private static String decode(String des) {
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                return new String(decoder.decodeBuffer(des));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }


}
